/**
 * 正则工具
 * @see RegexUtil.ts 
 */
export default class RegexUtil {

    /**
     * 判断字符是否为双字节字符（如中文字符）
     * @param string 原字符串
     */
    public static isDWORD(string: string): boolean {
        return /[^\x00-\xff]/.test(string);
    }

    /**
     * 判断字符是否为手机号
     * @param string 
     * @returns 
     */
    public static isPhoneNumber(string: string): boolean {
        return /^1[3|4|5|7|8]d{9}$/.test(string);
    }

    /**
     * 帐号是否合法(字母开头，允许5-16字节，允许字母数字下划线)
     * @param string 
     * @returns 
     */
    public static checkAccountValidity(string: string): boolean {
        return /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/.test(string);
    }

    /**
     * 密码(以字母开头，长度在6~18之间，只能包含字母、数字和下划线)
     * @param string 
     * @returns 
     */
    public static checkPasswordValidity(string: string): boolean {
        return /^[a-zA-Z]w{5,17}$/.test(string);
    }

    /**
     * 是否是中文
     * @param string 
     * @returns 
     */
    public static checkChina(string: string): boolean {
        if (/.*[\u4e00-\u9fa5]+.*$/.test(string)) {
            return true;
        }
        return false;
    }

    /**
     * 是否有特殊字符
     * @param string 
     * @param special 
     * @returns 
     */
    public static hasSpecial(string: string, special: string): boolean {
        let isSpecial = false;
        if (string) {
            for (let i = 0; i < string.length; i++) {
                let asc = string.charCodeAt(i);
                if (!(asc >= 48 && asc <= 57 || asc >= 65 && asc <= 90 || asc >= 97 && asc <= 122)) {
                    isSpecial = true;
                    if (special && string[i] == special) { //允许有可以用的特殊字符需要传入
                        isSpecial = false;
                        continue;
                    }
                    break;
                }
            }
        }
        return isSpecial;
    }

    /**
     * 是否是身份证号
     * @param id 
     * @returns 
     */
    public static isIdNumber(id : string) : boolean {
        var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
        var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
        var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
        var code = id.substring(17);
        if(p.test(id)) {
            var sum = 0;
            for(var i = 0; i < 17; i++) {
                let val = Number(id[i]);
                sum += val*factor[i];
            }
            if(parity[sum % 11] == code.toUpperCase()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取身份证年龄
     * @param idcard 
     * @returns 
     */
    public static getIDCardAge( idcard : string){
        if(idcard == "") return 0;

        let birthtime = idcard.substring(6, 14);
        let year = parseInt(birthtime.substring(0, 4));
        let month = parseInt(birthtime.substring(4, 6));
        let day = parseInt(birthtime.substring(6, 8));
        console.log(birthtime,year,month,day);

        let date = new Date();

        let age = date.getFullYear() - year;

        if(date.getMonth() > month){
            age++;
        }

        return age;
    }

    /**
     * 是否是url
     * @param str_url 
     * @returns 
     */
    public static isUrl(str_url : string) : boolean {
        var strRegex = "((https|http|ftp|rtsp|mms)?://)"
            + "(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
            + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
            + "|" // 允许IP和DOMAIN（域名）
            + "([0-9a-z_!~*'()-]+\\.)*" // 域名- www.
            + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 二级域名
            + "[a-z]{2,6})" // first level domain- .com or .museum
            + "(:[0-9]{1,4})?" // 端口- :80
            + "((/?)|" // a slash isn't required if there is no file name
            + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)";
        var re=new RegExp(strRegex);
        if (re.test(str_url)){
            return true;
        }
        else{
            return false;
        }
    }
}
